<template>
  <page-header-wrapper :title="false">
    <a-card title="二维码管理">
      <template slot="extra">
        <a-button style="margin-right:5px" size="small" type="primary" icon="plus" @click="addTask">添加评估案件</a-button>
        <a-button type="primary" size="small" icon="upload" @click="batchUpload">批量上传案件</a-button>
      </template>
      <div class="table-page-search-wrapper">
        <a-form-model
          layout="inline"
          ref="queryParam"
          :model="queryParam"
        >
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="报告标题">
                <a-input v-model="queryParam['filter[doc_title]']" placeholder="请输入报告标题"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="客户姓名">
                <a-input v-model="queryParam['filter[customer_name]']" placeholder="请输入客户姓名"/>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="录入时间">
                  <a-range-picker v-model="queryParam['input_time_start']" @change="onChange" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="评估师">
                  <a-select v-model="queryParam['filter[appraiser]']" placeholder="请选择评估师">
                    <a-select-option v-for="(item,index) in appraiserOpts" :key="index" :value="index">{{item}}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="所属事务所">
                  <a-input v-model="queryParam['filter[office]']" placeholder="请输入所属事务所" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="合同编号">
                  <a-input v-model="queryParam['filter[contract_no]']" placeholder="请输入合同编号" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="业务编号">
                  <a-input v-model="queryParam['filter[business_no]']" placeholder="请输入业务编号" />
                </a-form-model-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">搜索</a-button>
                <a-button style="margin-left: 8px" @click="resetForm">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="table-operator">
        <s-table
          bordered
          ref="table"
          size="middle"
          :row-key="(record) => record.id"
          :columns="taskColumns"
          :data="taskData"
        >
          <template slot="contract_no" slot-scope="text,record">
            {{text}}<br>
            {{record.business_no}}
          </template>
          <template slot="qr_code" slot-scope="text,record">
            <div v-if="text === ''">
              <a-button size="small" icon="edit" type="primary" @click="getQr(record.id)">生成二维码</a-button>
            </div>
            <div v-else>
              <img :src="text" width="100" height="100" />
              <div><a-button size="small" icon="download" type="link" @click="download(text)">下载</a-button></div>
            </div>
          </template>
          <template slot="action" slot-scope="text,record">
            <a-button size="small" icon="edit" type="danger" ghost @click="onEdit(record.id)">修改</a-button>
            <a-button size="small" icon="delete" type="danger" @click="onDel(record.id)">删除</a-button>
          </template>
        </s-table>
      </div>
    </a-card>
    <a-modal
      title="批量上传案件"
      v-model="visible2"
      :destroy-on-close="true"
      @ok="onSubmitUpload"
    >
      <a-form-model
        ref="uploadForm"
        :model="uploadForm"
      >
        <a-form-model-item label="上传表格">
          <a-upload
            name="file"
            accept=".csv"
            :action="uploadUrl"
            :headers="headers"
            @change="onChange1"
          >
            <a-button size="small" type="primary" icon="upload">上传回执文件</a-button>
          </a-upload>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
    <a-modal
      title="添加案件"
      width="600px"
      :confirm-loading="loading"
      v-model="visible"
      @ok="onSubmit"
      @cancel="onCancel"
    >
      <a-form-model
        ref="taskForm"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
        :model="taskForm"
        :rules="taskRules"
      >
        <a-form-model-item label="合同编号">
          <a-input v-model="taskForm.contract_no" type="text" />
        </a-form-model-item>
        <a-form-model-item label="业务编号">
          <a-input v-model="taskForm.business_no" type="text" />
        </a-form-model-item>
        <a-form-model-item label="报告标题">
          <a-input v-model="taskForm.doc_title" type="text" />
        </a-form-model-item>
        <a-form-model-item label="报告文号">
          <a-input v-model="taskForm.doc_num" type="text" />
        </a-form-model-item>
        <a-form-model-item label="客户姓名">
          <a-input v-model="taskForm.customer_name" type="text" />
        </a-form-model-item>
        <a-form-model-item label="录入时间">
          <a-date-picker v-model="taskForm.input_time" @change="onChangeTime" />
        </a-form-model-item>
        <a-form-model-item label="所属事务所">
          <a-input v-model="taskForm.office" type="text" />
        </a-form-model-item>
        <a-form-model-item label="中国注册资产评估师">
          <a-select mode="multiple" v-model="taskForm.appraiser">
            <a-select-option v-for="(item,index) in appraiserOpts" :key="index" :value="index">{{item}}</a-select-option>
          </a-select>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
import storage from 'store'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import apis from '@/api'
import taskColumns from '@/columns/pg.process'
import { batchUpload, delTask, getAddTask, getQrCode, getTaskList, postAddTask } from '@/api/pg.process'

export default {
  components: {
    STable
  },
  name: 'PgTask',
  data () {
    return {
      advanced: false,
      loading: false,
      labelCol: { span: 6 },
      wrapperCol: { span: 18 },
      taskColumns: taskColumns.task,
      taskData: parameters => {
        return getTaskList(Object.assign(parameters, this.queryParam)).then(res => {
          this.appraiserOpts = res.result.appraiserOpts
          return res.result
        })
      },
      appraiserOpts: {},
      visible: false,
      taskForm: {
        doc_num: '吉捷吉评字[2022]第***号',
        office: '北京吉捷吉评估有限公司'
      },
      taskRules: {},
      queryParam: {},
      headers: {
        'Access-Token': storage.get(ACCESS_TOKEN)
      },
      uploadUrl: apis.UploadFile,
      uploadForm: {},
      visible2: false
    }
  },
  methods: {
    moment,
    onEdit (id) {
      getAddTask(id).then(res => {
        this.taskForm = res.result.casesData
        this.taskForm.appraiser = res.result.appraiser
        this.visible = true
      })
    },
    onDel (id) {
      this.$confirm({
        title: '是否确认删除',
        onOk: () => {
          delTask(id).then(res => {
            this.$refs.table.refresh(true)
          })
        }
      })
    },
    addTask () {
      this.visible = true
    },
    batchUpload () {
      this.uploadForm = {}
      this.visible2 = true
    },
    onSubmit () {
      const app = this
      const data = {}
      app.loading = true
      for (const k in app.taskForm) {
        data['cases[' + k + ']'] = app.taskForm[k]
      }
      if (app.taskForm.id) {
        data['id'] = app.taskForm.id
        data['case[appraiser]'] =
        postAddTask(data).then(res => {
          app.$refs.table.refresh(true)
          app.loading = false
          app.onCancel()
        })
      } else {
        data['id'] = ''
        postAddTask(data).then(res => {
          app.$refs.table.refresh(true)
          app.loading = false
          app.onCancel()
        })
      }
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    resetForm () {
      this.queryParam = {}
      this.$refs.table.refresh(true)
    },
    onChange (date, dateString) {
      this.queryParam['filter[input_time_start]'] = dateString[0]
      this.queryParam['filter[input_time_end]'] = dateString[1]
    },
    onChange1 (info) {
      if (info.file.status === 'uploading') {
        return false
      }
      if (info.file.status === 'done') {
        this.uploadForm['file[value]'] = info.file.response.result.url
      }
    },
    onSubmitUpload () {
      batchUpload(this.uploadForm).then(res => {
        if (res.status === 0) {
          this.visible2 = false
          this.$refs.table.refresh()
        } else {
          this.$warning({
            title: res.msg
          })
        }
      })
    },
    onCancel () {
      this.taskForm = {
        doc_num: '吉捷吉评字[2022]第***号',
        office: '北京吉捷吉评估有限公司'
      }
      this.$refs.taskForm.resetFields()
      this.loading = false
      this.visible = false
    },
    onChangeTime (date, dateString) {
      this.taskForm['input_time'] = dateString
    },
    download (url) {
      window.open(url)
    },
    getQr (id) {
      const app = this
      getQrCode(id).then(res => {
        if (res.status === 0) {
          app.$refs.table.refresh()
        } else {
          app.$warning({
            title: res.msg
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
